package com.tear.language.utils;

import java.lang.reflect.Field;

public class TypeUtil {
	/**
	 * 根据属性类型的一般数据类型转化成其包装类
	 * @param str
	 */
	public static Object toWraperClass(Field field,Object value) {
		if(field.getType()==Integer.TYPE||field.getType()==Integer.class)
			return new Integer(value.toString());
		else if(field.getType()==Double.TYPE||field.getType()==Double.class)
			return new Double(value.toString());
		else if(field.getType()==Float.TYPE||field.getType()==Float.class)
			return new Float(value.toString());
		else if(field.getType()==Boolean.TYPE||field.getType()==Boolean.class)
			return new Boolean(value.toString());
		else if(field.getType()==Short.TYPE||field.getType()==Short.class)
			return new Short(value.toString());
		else if(field.getType()==Character.TYPE||field.getType()==Character.class)
			return new Character(value.toString().charAt(0));
		else
			return value;
	}
	
	//替换访问数组符号
	public static String replaceArray(String str) {
		/**
		 * 因为全局替换，可能引号里面的()都被替换成:{和}
		 * 	str = str.replace("(", ":{");
		 *	str = str.replace(")","}");
		 */
		str = str.replace(":{", "(");
		str = str.replace("}", ")");
		/**
		 * 替换因为之前全局替换，如果有访问数组或者集合如a[0]下面的替换会导致的多余的大括号，这里需要还原回去
		 * str = str.replace("[", "<-{");
		 * str = str.replace("]", "}->");
		 * str = str.replace("<-", "[");
		 * str = str.replace("->", "]");
		 */
		str = str.replace("[{", "[");
		str = str.replace("}]", "]");

		return str;
	}
	
	public static String replaceClass(String str) {
		str = str.replace("Integer.", "@java.lang.Integer@");
		str = str.replace("Long.", "@java.lang.Long@");
		str = str.replace("Float.", "@java.lang.Float@");
		str = str.replace("Double.", "@java.lang.Double@");
		str = str.replace("Short.", "@java.lang.Short@");
		str = str.replace("Byte.", "@java.lang.Byte@");
		str = str.replace("Math.", "@java.lang.Math@");
		str = str.replace("DateUtils.", "@com.project.base.node.utils.DateUtils@");
		str = str.replace("NumberUtils.", "@com.project.base.node.utils.NumberUtils@");
		str = str.replace("StringUtils.", "@org.apache.commons.lang.StringUtils@");
		str = str.replace("DigestUtils.", "@org.apache.commons.codec.digest.DigestUtils@");
		return str;
	}
}
